package com.ruoyi.bearing.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.exception.base.BaseException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bearing.domain.OptimizedModel;
import com.ruoyi.bearing.service.IOptimizedModelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 最优诊断与预测模型Controller
 */
@RestController
@RequestMapping("/bearing/model")
public class OptimizedModelController extends BaseController {
    @Autowired
    private IOptimizedModelService optimizedModelService;

    /**
     * 查询最优诊断与预测模型列表
     */
    @PreAuthorize("@ss.hasPermi('bearing:model:list')")
    @GetMapping("/list")
    public TableDataInfo list(OptimizedModel optimizedModel) {
        startPage();
        List<OptimizedModel> list = optimizedModelService.selectOptimizedModelList(optimizedModel);
        return getDataTable(list);
    }

    /**
     * 导出最优诊断与预测模型列表
     */
    @PreAuthorize("@ss.hasPermi('bearing:model:export')")
    @Log(title = "最优诊断与预测模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OptimizedModel optimizedModel) {
        List<OptimizedModel> list = optimizedModelService.selectOptimizedModelList(optimizedModel);
        ExcelUtil<OptimizedModel> util = new ExcelUtil<OptimizedModel>(OptimizedModel.class);
        util.exportExcel(response, list, "最优诊断与预测模型数据");
    }

    /**
     * 获取最优诊断与预测模型详细信息
     */
    @PreAuthorize("@ss.hasPermi('bearing:model:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(optimizedModelService.selectOptimizedModelById(id));
    }

    /**
     * 新增最优诊断与预测模型
     */
    @PreAuthorize("@ss.hasPermi('bearing:model:add')")
    @Log(title = "最优诊断与预测模型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OptimizedModel optimizedModel) {
        List<OptimizedModel> optimizedModelList = optimizedModelService.selectOptimizedModelList(new OptimizedModel());
        if(optimizedModelList.size() > 0){
            optimizedModel.setId(optimizedModelList.get(0).getId());
            return toAjax(optimizedModelService.updateOptimizedModel(optimizedModel));
        }
        return toAjax(optimizedModelService.insertOptimizedModel(optimizedModel));
    }

    /**
     * 修改最优诊断与预测模型
     */
    @PreAuthorize("@ss.hasPermi('bearing:model:edit')")
    @Log(title = "最优诊断与预测模型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OptimizedModel optimizedModel) {
        return toAjax(optimizedModelService.updateOptimizedModel(optimizedModel));
    }

    /**
     * 删除最优诊断与预测模型
     */
    @PreAuthorize("@ss.hasPermi('bearing:model:remove')")
    @Log(title = "最优诊断与预测模型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(optimizedModelService.deleteOptimizedModelByIds(ids));
    }
}
